from django.http import HttpResponse
from django.shortcuts import render
from django.template import loader


class Person:
    def __init__(self, name, age):
        self.name = name
        self.age = age


def page_2003_view(request):
    html = '<h1>这是我的第一个页面</h1>'
    return HttpResponse(html)


def page_1(request):
    html = '<h1>这是编号为1的网页</h1>'
    return HttpResponse(html)


def page_2(request):
    html = '<h1>这是编号为2的网页</h1>'
    return HttpResponse(html)


def index_view(request):
    # t = loader.get_template('index.html')
    # html = t.render()
    # return HttpResponse(html)
    username = '张三'
    password = '123456'
    age = 19
    list1 = ['chongqing', 'beijing', 'guangzhou', 'hejin', 'yuci']
    dict1 = {'name': 'lkp', 'age': 18}
    p1 = Person('lkp', 18)

    def func():
        return ("这是函数返回iid结果")

    #                                                根据局部变量生成字典
    return render(request, 'index.html', locals())


def page_view(request, pg):
    html = f"这是我的第{pg}个网页!"
    return HttpResponse(html)


def page_math(request, num1, str1, num2):
    if str1 not in ["add", "mul", "sub"]:
        return HttpResponse("Your str is wrong!")
    result = 0
    if str1 == "add":
        result = num1 + num2
    elif str1 == "sub":
        result = num1 - num2
    elif str1 == "mul":
        result = num1 * num2
    return HttpResponse('结果为:%s' % result)


def birthday_show(request, year, month, day):
    return HttpResponse(f"生日为:{year}年{month}月{day}日")


def test_request(request):
    print("path_info is", request.path_info)
    print("method is", request.method)
    print("GET is", request.GET)
    print("full path is", request.get_full_path())
    return HttpResponse("test request is ok")


def test_view(request):
    if request.method == "GET":
        data = request.GET
        print(data)
        print(data.get('age'))
        print(data.getlist('answer'))
        return HttpResponse("ok")
    elif request.method == "POST":
        data = request.POST
        print(data)
        name = data.get("username")
        return HttpResponse(f'欢迎{name}回来！')


def calc_view(request):
    if request.method == 'GET':
        return render(request, 'calc.html')
    elif request.method == 'POST':

        x = int(request.POST.get('x'))
        op = request.POST.get('op')
        y = int(request.POST.get('y'))
        if op == 'add':
            result = x + y
        elif op == 'sub':
            result = x - y
        elif op == 'mul':
            result = x * y
        elif op == 'div':
            result = x / y
        return render(request, 'calc.html', locals())


def base_view(request):
    return render(request, 'base.html')


def free_view(request):
    return render(request, 'free.html')


def advanced_view(request):
    return render(request,'advanced.html')


def live_view(request):
    return render(request,'live.html')